package com.example.dynamicschedule.task;

import com.example.dynamicschedule.bean.CkCreditOutCompanyDataRelation;
import com.example.dynamicschedule.bean.CkCreditOutcompanyCommission;
import com.example.dynamicschedule.bean.CkCreditOutcompanyFixedAgent;
import com.example.dynamicschedule.dao.CkCreditOutCompanyDataRelationMapper;
import com.example.dynamicschedule.dao.CkCreditOutcompanyCommissionMapper;
import com.example.dynamicschedule.dao.CkCreditOutcompanyFixedAgentMapper;
import com.example.dynamicschedule.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Component
public class CreditOutcompanyCommissionSumTask {
    static Logger log = Logger.getLogger(CreditOutcompanyCommissionSumTask.class);
    @Autowired
    private CkCreditOutcompanyCommissionMapper ckCreditOutcompanyCommissionMapper;
    @Autowired
    private CkCreditOutcompanyFixedAgentMapper ckCreditOutcompanyFixedAgentMapper;
    @Autowired
    private DateUtils dateUtils;
    @Autowired
    private CkCreditOutCompanyDataRelationMapper ckCreditOutCompanyDataRelationMapper;

    /**
     * 每月计算固定代理类和佣金类总计
     */
    @Transactional
    public void CreditOutcompanyCommissionSumTask() {
        log.info("计算佣金总计-------------------------开始");
        String preLikeBatchNumber = /*dateUtils.getPreLikeBatchNumber()*/"%202012%";
        log.info("计算佣金总计-----------获取委外公司佣金表--------------开始");
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("batchNumber",preLikeBatchNumber);
        List<Integer> checkModels = new ArrayList<>();
        checkModels.add(1);
        checkModels.add(2);
        paramsMap.put("checkModels",checkModels);
        List<CkCreditOutCompanyDataRelation> ckCreditOutCompanyDataRelationList= ckCreditOutCompanyDataRelationMapper.selectListByBatchNumberAndCheckModel(paramsMap);
        log.info("计算佣金总计-----------获取委外公司佣金表--------------结束");
        if (CollectionUtils.isNotEmpty(ckCreditOutCompanyDataRelationList)) {
            log.info("计算佣金总计-----------整理委外公司佣金表--------------开始");
            Map<String, Map<String,List<CkCreditOutCompanyDataRelation>>> ckCreditOutCompanyDataRelationMap= getckCreditOutCompanyDataRelationMap(ckCreditOutCompanyDataRelationList);
            log.info("计算佣金总计-----------整理委外公司佣金表--------------结束");
            log.info("计算佣金总计-----------获取每天佣金表--------------开始");
            List<CkCreditOutcompanyCommission> ckCreditOutcompanyCommissions=ckCreditOutcompanyCommissionMapper.selectLikeBatchNumber(preLikeBatchNumber);
            log.info("计算佣金总计-----------获取每天佣金表--------------结束");
            log.info("计算佣金总计-----------获取每天固定代理类表--------------开始");
            List<CkCreditOutcompanyFixedAgent> ckCreditOutcompanyFixedAgents=ckCreditOutcompanyFixedAgentMapper.selectLikeBatchNumber(preLikeBatchNumber);
            log.info("计算佣金总计-----------获取每天固定代理类表--------------结束");
            Map<String, Map<String,List<CkCreditOutcompanyCommission>>> ckCreditOutcompanyCommissionMap = null;
            if (CollectionUtils.isNotEmpty(ckCreditOutcompanyCommissions)) {
                log.info("计算佣金总计-----------整理佣金类--------------开始");
                ckCreditOutcompanyCommissionMap=getCkCreditOutcompanyCommissionMap(ckCreditOutcompanyCommissions);
                log.info("计算佣金总计-----------整理佣金类--------------结束");
            } else {
                log.info("计算佣金总计-----------获取每天佣金表--------------为空");
            }
            Map<String, Map<String,List<CkCreditOutcompanyFixedAgent>>> ckCreditOutcompanyFixedAgentMap = null;
            if (CollectionUtils.isNotEmpty(ckCreditOutcompanyFixedAgents)) {
                log.info("计算佣金总计-----------整理固定代理类类--------------开始");
                ckCreditOutcompanyFixedAgentMap=getckCreditOutcompanyFixedAgentMap(ckCreditOutcompanyFixedAgents);
                log.info("计算佣金总计-----------整理固定代理类类--------------结束");
            } else {
                log.info("计算佣金总计-----------获取每天固定代理类表--------------为空");
            }
            if (CollectionUtils.isNotEmpty(ckCreditOutcompanyFixedAgents)
                    || CollectionUtils.isNotEmpty(ckCreditOutcompanyCommissions)) {
                log.info("计算佣金总计-----------计算回佣率--------------开始");
                ckCreditOutCompanyDataRelationList= updateCreditOutcompanyCommissionSum(ckCreditOutCompanyDataRelationMap,
                        ckCreditOutcompanyFixedAgentMap,ckCreditOutcompanyCommissionMap);
                log.info("计算佣金总计-----------计算回佣率--------------结束");
                log.info("计算佣金总计-----------更新回佣列表--------------开始");
                if (CollectionUtils.isNotEmpty(ckCreditOutCompanyDataRelationList)) {
                    ckCreditOutCompanyDataRelationMapper.updateForEach(ckCreditOutCompanyDataRelationList);
                } else {

                    log.info("计算佣金总计-----------更新回佣列表--------------------为空");

                }
                log.info("计算佣金总计-----------更新回佣列表--------------------结束");
            }


        } else {
            log.info("计算佣金总计-----------获取委外公司佣金表--------------为空");
        }
        log.info("计算佣金总计-------------------------结束");

    }

    /**
     * 计算佣金开始
     * @param ckCreditOutCompanyDataRelationMap
     * @param ckCreditOutcompanyFixedAgentMap 固定代理类
     * @param ckCreditOutcompanyCommissionMap  回佣
     * @return
     */
    private List<CkCreditOutCompanyDataRelation> updateCreditOutcompanyCommissionSum(Map<String, Map<String, List<CkCreditOutCompanyDataRelation>>> ckCreditOutCompanyDataRelationMap, Map<String, Map<String, List<CkCreditOutcompanyFixedAgent>>> ckCreditOutcompanyFixedAgentMap, Map<String, Map<String, List<CkCreditOutcompanyCommission>>> ckCreditOutcompanyCommissionMap) {
        List<CkCreditOutCompanyDataRelation> ckCreditOutCompanyDataRelations = new ArrayList<>();
        for (Map.Entry<String, Map<String, List<CkCreditOutCompanyDataRelation>>> stringMapEntry : ckCreditOutCompanyDataRelationMap.entrySet()) {
            String batchNumber = stringMapEntry.getKey();
            Map<String, List<CkCreditOutCompanyDataRelation>> value = stringMapEntry.getValue();
            for (Map.Entry<String, List<CkCreditOutCompanyDataRelation>> stringListEntry : value.entrySet()) {
                String outCompany = stringListEntry.getKey();
                List<CkCreditOutCompanyDataRelation> value1 = stringListEntry.getValue();
                for (CkCreditOutCompanyDataRelation ckCreditOutCompanyDataRelation : value1) {
                    String checkModel = ckCreditOutCompanyDataRelation.getCheckModel();
                    Double commission=0.00;
                    if ("1".equalsIgnoreCase(checkModel)) {
                        Map<String, List<CkCreditOutcompanyFixedAgent>> stringListMap = ckCreditOutcompanyFixedAgentMap.get(batchNumber);
                        if (stringListMap != null) {
                            List<CkCreditOutcompanyFixedAgent> ckCreditOutcompanyFixedAgents = stringListMap.get(outCompany);
                            if (CollectionUtils.isNotEmpty(ckCreditOutcompanyFixedAgents)) {
                                for (CkCreditOutcompanyFixedAgent ckCreditOutcompanyFixedAgent : ckCreditOutcompanyFixedAgents) {
                                    Double c = ckCreditOutcompanyFixedAgent.getCommission();
                                    commission = new BigDecimal(c).add(new BigDecimal(commission)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                                }
                            } else {
                                log.info("计算佣金总计-----------计算固定代理类--------------回佣列表为空,批次号为:"+batchNumber+";委外公司为:"+outCompany);
                                continue;
                            }
                        } else {
                            log.info("计算佣金总计-----------计算固定代理类--------------回佣列表为空,批次号为:"+batchNumber+";委外公司为:"+outCompany);
                            continue;
                        }

                    } else if ("2".equalsIgnoreCase(checkModel)) {
                        Map<String, List<CkCreditOutcompanyCommission>> stringListMap = ckCreditOutcompanyCommissionMap.get(batchNumber);
                        if (stringListMap != null) {
                            List<CkCreditOutcompanyCommission> ckCreditOutcompanyFixedAgents = stringListMap.get(outCompany);
                            if (CollectionUtils.isNotEmpty(ckCreditOutcompanyFixedAgents)) {
                                for (CkCreditOutcompanyCommission ckCreditOutcompanyFixedAgent : ckCreditOutcompanyFixedAgents) {
                                    Double c = ckCreditOutcompanyFixedAgent.getCommission();
                                    commission = new BigDecimal(c).add(new BigDecimal(commission)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                                }
                            } else {
                                log.info("计算佣金总计-----------计算回佣率--------------回佣列表为空,批次号为:"+batchNumber+";委外公司为:"+outCompany);
                                continue;
                            }
                        } else {
                            log.info("计算佣金总计-----------计算回佣率--------------回佣列表为空,批次号为:"+batchNumber+";委外公司为:"+outCompany);
                            continue;
                        }

                    }else {
                        log.info("计算佣金总计-----------计算其他--------------计算类型为其他");

                    }
                    if (commission != 0.00) {
                        ckCreditOutCompanyDataRelation.setBrokerage(commission);
                        ckCreditOutCompanyDataRelation.setIsCalculated(1);
                        ckCreditOutCompanyDataRelations.add(ckCreditOutCompanyDataRelation);
                    }
                }
            }
        }
        return ckCreditOutCompanyDataRelations;
    }

    /**
     * 整理委外佣金表集合
     * @param ckCreditOutCompanyDataRelationList
     * @return
     */
    private Map<String, Map<String, List<CkCreditOutCompanyDataRelation>>> getckCreditOutCompanyDataRelationMap(List<CkCreditOutCompanyDataRelation> ckCreditOutCompanyDataRelationList) {
        Map<String, Map<String,List<CkCreditOutCompanyDataRelation>>> ckCreditOutcompanyCommissionMap = new HashMap<>();
        for (CkCreditOutCompanyDataRelation ckCreditOutCompanyDataRelation : ckCreditOutCompanyDataRelationList) {
            String batchNumber = ckCreditOutCompanyDataRelation.getBatchNumber();
            Map<String,List<CkCreditOutCompanyDataRelation>> outCompanyMap = new HashMap<>();
            if (ckCreditOutcompanyCommissionMap.get(batchNumber) != null) {
                outCompanyMap = ckCreditOutcompanyCommissionMap.get(batchNumber);
            }
            String outCompant = ckCreditOutCompanyDataRelation.getOutsourcingCompany();
            List<CkCreditOutCompanyDataRelation> ckCreditOutcompanyCommissionList = new ArrayList<>();
            if (outCompanyMap.get(outCompant) != null) {
                ckCreditOutcompanyCommissionList = outCompanyMap.get(outCompant);
            }
            ckCreditOutcompanyCommissionList.add(ckCreditOutCompanyDataRelation);
            outCompanyMap.put(outCompant,ckCreditOutcompanyCommissionList);
            ckCreditOutcompanyCommissionMap.put(batchNumber,outCompanyMap);
        }
        return ckCreditOutcompanyCommissionMap;

    }

    /**
     * 整理固定代理类集合
     * @param ckCreditOutcompanyFixedAgents
     * @return
     */
    private Map<String, Map<String, List<CkCreditOutcompanyFixedAgent>>> getckCreditOutcompanyFixedAgentMap(List<CkCreditOutcompanyFixedAgent> ckCreditOutcompanyFixedAgents) {
        Map<String, Map<String,List<CkCreditOutcompanyFixedAgent>>> ckCreditOutcompanyCommissionMap = new HashMap<>();
        for (CkCreditOutcompanyFixedAgent ckCreditOutcompanyFixedAgent : ckCreditOutcompanyFixedAgents) {
            String batchNumber = ckCreditOutcompanyFixedAgent.getBatchNumber();
            Map<String,List<CkCreditOutcompanyFixedAgent>> outCompanyMap = new HashMap<>();
            if (ckCreditOutcompanyCommissionMap.get(batchNumber) != null) {
                outCompanyMap = ckCreditOutcompanyCommissionMap.get(batchNumber);
            }
            String outCompant = ckCreditOutcompanyFixedAgent.getOutCompant();
            List<CkCreditOutcompanyFixedAgent> ckCreditOutcompanyCommissionList = new ArrayList<>();
            if (outCompanyMap.get(outCompant) != null) {
                ckCreditOutcompanyCommissionList = outCompanyMap.get(outCompant);
            }
            ckCreditOutcompanyCommissionList.add(ckCreditOutcompanyFixedAgent);
            outCompanyMap.put(outCompant,ckCreditOutcompanyCommissionList);
            ckCreditOutcompanyCommissionMap.put(batchNumber,outCompanyMap);
        }
        return ckCreditOutcompanyCommissionMap;
    }

    /**
     * 整理佣金集合
     * @param ckCreditOutcompanyCommissions
     * @return
     */
    private Map<String, Map<String, List<CkCreditOutcompanyCommission>>> getCkCreditOutcompanyCommissionMap(List<CkCreditOutcompanyCommission> ckCreditOutcompanyCommissions) {
        Map<String, Map<String,List<CkCreditOutcompanyCommission>>> ckCreditOutcompanyCommissionMap = new HashMap<>();
        for (CkCreditOutcompanyCommission ckCreditOutcompanyCommission : ckCreditOutcompanyCommissions) {
            String batchNumber = ckCreditOutcompanyCommission.getBatchNumber();
            Map<String,List<CkCreditOutcompanyCommission>> outCompanyMap = new HashMap<>();
            if (ckCreditOutcompanyCommissionMap.get(batchNumber) != null) {
                outCompanyMap = ckCreditOutcompanyCommissionMap.get(batchNumber);
            }
            String outCompant = ckCreditOutcompanyCommission.getOutCompant();
            List<CkCreditOutcompanyCommission> ckCreditOutcompanyCommissionList = new ArrayList<>();
            if (outCompanyMap.get(outCompant) != null) {
                ckCreditOutcompanyCommissionList = outCompanyMap.get(outCompant);
            }
            ckCreditOutcompanyCommissionList.add(ckCreditOutcompanyCommission);
            outCompanyMap.put(outCompant,ckCreditOutcompanyCommissionList);
            ckCreditOutcompanyCommissionMap.put(batchNumber,outCompanyMap);
        }
        return ckCreditOutcompanyCommissionMap;
    }
}
